<?php
@session_start();
include("../inc/pChart/class/pData.class.php");
include("../inc/pChart/class/pDraw.class.php");
include("../inc/pChart/class/pImage.class.php");

$i = 0;
$data_antes = "";
foreach ($_SESSION[xml] as $value) {//limite de registros em 384
    if($_REQUEST[tipo] == "mensal"){// mensal
        if (substr($value[data], 3) != $data_antes) {
            $totais[$i] = 0 + $value[total];
            $datas[$i] = substr($value[data], 3);
            $data_antes = substr($value[data], 3);
        }
    }else if($_REQUEST[tipo] == "diario"){// diario
        if (substr($value[data], 0, -5) != $data_antes) {
            $totais[$i] = 0 + $value[total];
            $datas[$i] = substr($value[data], 0, -5);
            $data_antes = substr($value[data], 0, -5);
        }
    }else if($_REQUEST[tipo] == "horario"){//intraday por hora
        if (substr($value[data].$value[hora], 0, -6) != $data_antes) {
            $totais[$i] = 0 + $value[total];
            $datas[$i] = $value[hora];
            $data_antes = $value[data].substr($value[hora], 0, -6);
        }
    }
    $i++;
}

$quantidade = 32;//limite de registros em 384
if(count($datas) <= 384 && count($datas) >= 1){//limite de registros em 384
    $quantidade = count($datas);
}

$totais = array_slice($totais, 0, $quantidade);
$datas= array_slice($datas, 0, $quantidade);

//$totais = array_reverse($totais);
//$datas = array_reverse($datas);

//$j = 0;
//while ($j <= $quantidade) {
//    if($j%2 == 0){
//        $totais[$j] = $j*1000;
//    }else{
//        $totais[$j] = $j*-1000;
//    }
//    $datas[$j] = date("H:i:s", $j*1000);
//    $j++;
//     
//}

$myData = new pData();
$myData->addPoints($totais,"Serie1");
$myData->setSerieDescription("Serie1","Valores Totais");
$myData->setSerieOnAxis("Serie1",0);

$myData->addPoints($datas,"Absissa");
$myData->setAbscissa("Absissa");

$myData->setAxisPosition(0,AXIS_POSITION_LEFT);
$myData->setAxisName(0,"Valores");
$myData->setAxisUnit(0,"");
$myData->setAxisDisplay(0,AXIS_FORMAT_CURRENCY);



$w=640;
if($quantidade*20 >= $w){
    $w=$quantidade*20;
}
$h=570;
$myPicture = new pImage($w,$h,$myData,TRUE);
$myPicture->Antialias = FALSE;
$Settings = array("StartR"=>224, "StartG"=>240, "StartB"=>255, "EndR"=>3, "EndG"=>3, "EndB"=>166, "Alpha"=>30);
$myPicture->drawGradientArea(0,0,$w,$h,DIRECTION_HORIZONTAL,$Settings);

$myPicture->drawRectangle(0,0,$w-1,$h-1,array("R"=>0,"G"=>0,"B"=>0));

$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>50,"G"=>50,"B"=>50,"Alpha"=>20));

$myPicture->setFontProperties(array("FontName"=>"../inc/pChart/fonts/Forgotte.ttf","FontSize"=>14));
$TextSettings = array("Align"=>TEXT_ALIGN_TOPLEFT
, "R"=>21, "G"=>97, "B"=>230, "DrawBox"=>1, "BoxAlpha"=>30);
$myPicture->drawText(60,15,"Extrato Total",$TextSettings);

$myPicture->setShadow(FALSE);
$myPicture->setGraphArea(80,70,$w-30,$h-45);
$myPicture->setFontProperties(array("R"=>0,"G"=>0,"B"=>0,"FontName"=>"../inc/pChart/fonts/Bedizen.ttf","FontSize"=>8));

//substituir o valor de max e min da scala por maior valor do arrary e menor valor do array
$AxisBoundaries = array(0=>array("Min"=>min($totais),"Max"=>max($totais)));
$Settings = array("Pos"=>SCALE_POS_LEFTRIGHT
, "Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries
, "LabelingMethod"=>LABELING_ALL
, "GridR"=>208, "GridG"=>208, "GridB"=>208, "GridAlpha"=>50, "TickR"=>240, "TickG"=>25, "TickB"=>10, "TickAlpha"=>50, "LabelRotation"=>45, "CycleBackground"=>1, "DrawArrows"=>1, "DrawXLines"=>FALSE, "DrawYLines"=>ALL);
$myPicture->drawScale($Settings);

$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>50,"G"=>50,"B"=>50,"Alpha"=>10));

$myPicture->drawSplineChart();

//$Config = array("R"=>0, "G"=>0, "B"=>0, "Alpha"=>50, "AxisID"=>0, "Ticks"=>4, "Caption"=>"0");
//$myPicture->drawThreshold(0,$Config);

$Config = array("PlotBorder"=>TRUE,"BorderSize"=>1,"Surrounding"=>-60,"BorderAlpha"=>80);
$myPicture->drawPlotChart($Config);

$Config = array("FontR"=>10, "FontG"=>10, "FontB"=>255, "FontName"=>"../inc/pChart/fonts/Bedizen.ttf", "FontSize"=>8, "Margin"=>6, "Alpha"=>30, "BoxSize"=>5, "Style"=>LEGEND_ROUND
, "Mode"=>LEGEND_HORIZONTAL
);
$myPicture->drawLegend(696,16,$Config);

$line = "/adminsoft/downloads/grafico_linhas.php?tipo=".$tipo;
$myPicture->autoOutput("grafico_linhas", $line);

?>